package cn.com.egova.erp.dao.impl;

import cn.com.egova.erp.dao.IReportDao;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by yuanchao on 2020/6/9.
 */
public class ReportDao extends HibernateDaoSupport implements IReportDao {

    @Override
    public List<Map<String, Object>> ordersReport(Date startDate, Date endDate) {
        String hql = "SELECT new Map(gt.name as name, SUM(od.money) as money) " +
                "FROM Goodstype gt, Goods g, Orders o, Orderdetail od " +
                "WHERE g.goodstype = gt and od.orders = o and od.goodsuuid = g.uuid and o.type = '2'";

        List<Date> dateList = new ArrayList<>();
        if (null != startDate) {
            hql += " and o.createtime >=? ";
            dateList.add(startDate);
        }
        if (null != endDate) {
            hql += " and o.createtime <=? ";
            dateList.add(endDate);
        }
        hql += " group by gt.name";
        //指定转换类型
        Date[] param = new Date[dateList.size()];
        Date[] params = dateList.toArray(param);
        return (List<Map<String, Object>>) getHibernateTemplate().find(hql, params);
    }

    @Override
    public List<Map<String, Object>> getSumMoney(int year) {
        String hql = "select new Map(month(o.createtime) || '月' as month, sum(od.money) as money) " +
                "from Orderdetail od, Orders o " +
                "where od.orders = o and o.type = '2' and year(o.createtime) = ? " +
                "group by month(o.createtime)";
        return (List<Map<String, Object>>) this.getHibernateTemplate().find(hql, year);
    }
}
